﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace UtfprLibrary.Windows.Forms.ComponentModel {
    /// <summary>
    /// TODO
    /// </summary>
    [Serializable()]
    [TypeConverter("UtfprLibrary.Windows.Forms.ComponentModel.UtfprValidatableControlConverter, UtfprLibrary.WindowsForms.Design")]
    public class UtfprValidatableControl {
        private Control _control;
        /// <summary>
        /// TODO
        /// </summary>
        [Browsable(false)]
        public Control Control {
            get { return _control; }
            set { _control = value; }
        }

        private string _errorMessage;
        /// <summary>
        /// TODO
        /// </summary>
        [Category("Behavior")]
        [Description("The error message.")]
        public string ErrorMessage {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        //private object _initialValue;
        ///// <summary>
        ///// TODO
        ///// </summary>
        //[Category("Behavior")]
        //[Description("The base value for validation. Default is null.")]
        //public object InitialValue {
        //    get { return _initialValue; }
        //    set { _initialValue = value; }
        //}

        /// <summary>
        /// TODO
        /// </summary>
        public UtfprValidatableControl() { }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="control"></param>
        /// <param name="error"></param>
        /// <param name="initialValue"></param>
        public UtfprValidatableControl(Control control, string error/*, object initialValue*/) {
            this._control = control;
            this._errorMessage = error;
            //this._initialValue = initialValue;
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            if (this.Control != null) {
                //return this.Control.Name;

                //http://stackoverflow.com/questions/3946079/from-a-usercontrol-code-behind-in-design-mode-how-do-i-get-all-the-controls-na
                return (string)TypeDescriptor.GetProperties(this.Control)["Name"].GetValue(this.Control);
            }
            return "err";
        }
    }
}
